//
//  HLIMKitQuoteView.m
//  YueYue
//
//  Created by hulu001 on 2024/1/12.
//

#import "HLIMKitQuoteView.h"

@implementation HLIMKitQuoteView


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [[UIColor colorWithRed:148/255.0 green:106/255.0 blue:250/255.0 alpha:1.0] colorWithAlphaComponent:0.1];
        self.layer.cornerRadius = 10;
        self.layer.masksToBounds = YES;
    }
    return self;
}

- (void)setMessage:(NSString *)message
{
    _message = message;
    
    [self addSubview:self.messageLabel];
    [self addSubview:self.closeButton];
    
    self.messageLabel.text = message;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    self.messageLabel.left = 12;
    self.messageLabel.height = self.height - 6;
    self.messageLabel.width = self.width - 12 - 41;
    self.messageLabel.centerY = self.middleY;
    
    self.closeButton.centerY = self.middleY;
    self.closeButton.right = self.width - 12;
}

- (UILabel *)messageLabel
{
    if (!_messageLabel) {
        _messageLabel = [UILabel.alloc initWithFrame:CGRectZero];
        _messageLabel.font = [UIFont systemFontOfSize:13];
        _messageLabel.numberOfLines = 2;
        _messageLabel.textAlignment = NSTextAlignmentLeft;
        _messageLabel.textColor = [UIColor colorWithRed:148/255.0 green:106/255.0 blue:250/255.0 alpha:1.0];
    }
    return _messageLabel;
}

- (UIButton *)closeButton
{
    if (!_closeButton) {
        _closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _closeButton.viewSize = CGSizeMake(12, 12);
        [_closeButton setImage:[UIImage imageNamed:@"input_close"] forState:UIControlStateNormal];
        [_closeButton addTarget:self action:@selector(event:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _closeButton;
}

- (CGSize)sizeThatFits:(CGSize)size
{
    NSString *msg = _message?:@"";
    
    CGFloat msgH = [msg boundingRectWithSize:CGSizeMake(size.width - 53, 40) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13]} context:nil].size.height;
    
    return CGSizeMake(size.width, msgH + 6);
}

- (void)event:(id)sender
{
    if (@protocol(HLIMKitQuoteViewDelegate) && [self.delegate respondsToSelector:@selector(imkit_quoteViewDidClose)]) {
        [self.delegate imkit_quoteViewDidClose];
    }
}

@end
